using System;
using System.Collections.Generic;
using System.Threading;
using MediaBrowser.Library;
using MediaBrowserWS.Service;
using MediaBrowserWS.Code;
using MediaBrowserWS.Logging;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.Hosting;

namespace MediaBrowserWS
{
    public class MyAddIn : IAddInModule, IAddInEntryPoint
    {
        private EventWaitHandle m_waitHandle = new ManualResetEvent(false);

        public void Initialize(Dictionary<string, object> appInfo, Dictionary<string, object> entryPointInfo)
        {
            try
            {
                //  Sets the wait handle to the launch method will not exit
                m_waitHandle.Reset();
            }
            catch (Exception ex)
            {
                Logger.Instance.LogMessage("Error initialising addin; ERROR: {0}", ex.Message);
                throw;
            }
        }

        public void Uninitialize()
        {
            try
            {
                //  Allow our launch method to exit
                m_waitHandle.Set();
            }
            catch (Exception ex)
            {
                Logger.Instance.LogMessage("Error uninitialising addin; ERROR: {0}", ex.Message);
                throw;
            }
        }

        public void Launch(AddInHost host)
        {
            //ShowDialog(host, "Attach to process now...", 15);
            AppSettings.Initialise();

            Logger.Instance.LogMessage("Attempting service start");
            try
            {
                MediaBrowserService service = new MediaBrowserService();
                service.Initialise(AppSettings.Address(GetPort()));

                if (AppSettings.ShowLoadSuccess)
                {
                    ShowDialog(host, "Success loading MediaBrowser Web Service", 2);
                }
                Logger.Instance.LogMessage("Success loading service");

                m_waitHandle.WaitOne();

                if (host != null && host.ApplicationContext != null)
                {
                    host.ApplicationContext.SingleInstance = true;
                }

                Application app = new Application(host);
                app.Start();
                
                Logger.Instance.LogMessage("Plugin successfully loaded");
            }
            catch (Exception ex)
            {
                Logger.Instance.LogMessage(ex.Message);
                throw;
            }
        }

        private int GetPort()
        {
            return AppSettings.BasePort;
        }

        public static void ShowDialog(AddInHost host, string strClickedText, int timeout)
        {
            bool modal = true;
            string caption = Resources.DialogCaption;

            if (host != null)
            {
                host.MediaCenterEnvironment.Dialog(strClickedText,
                                              caption,
                                              new object[] { DialogButtons.Ok },
                                              timeout,
                                              modal,
                                              null,
                                              delegate(DialogResult dialogResult) { });
            }
        }
    }
}